<!--
*@Author: QWP
*@Description: 发送后消息的操作
*@Date: 2025-01-16 15:36:15
-->
<template>
  <div class='f-position-relative f-margin-10'>
    <u-popover placement="top"
      :menus="msgEditMenu"
      @click="(value, menu) => msgEditMenuClick(value, menu)">
      <u-icon-font type="icon-more-fill" 
        v-if="data.charPosition == 'right'"
        class="f-abs-center"
        style="color: #999">
      </u-icon-font>
    </u-popover>
  </div>
</template>

<script lang='ts' setup>
import { ref, reactive, computed, onMounted } from 'vue' 
import { useChatStore } from '@/store'
import { storeToRefs } from 'pinia'
import { alertC, messageC } from '@/util/common'
import * as chatApi from '@/service/emergency/chat'
import dayjs from 'dayjs'

const props = defineProps<{
  group: object,
  data: object,
  index: number
}>()

const emits = defineEmits(['success'])

const chatStore = useChatStore()
const { recallMsgs } = storeToRefs(chatStore)

// 聊天内容的右键
const msgEditMenu = [
  {label: '撤回', value: 'recall'}
]
const msgEditMenuClick = (value, menu?: obejct) => {
  switch(value) {
    case 'recall':
      recallMessage(props.data, props.index, value);
      break
  } 
}

// 撤回消息
const recallMessage = async (data, rowIndex, type) => {
  try {
    await alertC('确认撤回这条消息？', 'confirm')
    const form = {
      keyId: props.group['keyid'],
      messageId: data['keyid'],
      status: '2', // 1 发送   2 撤回
      types: props.group['types']
    }
    const res = await chatApi.recallMessage(form);
    // 撤回成功且是文本消息时，将撤回的消息缓存五分钟可以编辑
    if(res.code == 0 && data['messageType'] == 1) {

      storeLocalMsg(data)

      emits('success', {
        type,
        ...props,
        data: {
          message: '你撤回了一条消息',
          sourceMessage: JSON.stringify({
            message: { ...data, status: 3 }
          })
        }
      })
    }
    else {
      messageC(res.msg)
    }
  } catch (error) {}
}


// 缓存和获取撤回消息
const storeLocalMsg = (data?: object) => {
  const storeData = recallMsgs.value;

  storeData[data['keyid']] = {
    ...data,
    clearDate: dayjs().add(1, 'minute').format('YYYY-MM-DD HH:mm:ss')
  };
  chatStore.setRecallMsgs(storeData)
}

</script>

<style lang='scss' scoped>
</style>